package com.zzl.ov_library.controller;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzl.ov_library.dto.Result;
import com.zzl.ov_library.entity.User;
import com.zzl.ov_library.service.UserService;
import com.zzl.ov_library.tools.WechatTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private WechatTools wechatTools;

    @PostMapping("/getWxInfo")
    public String getWxInfo(@RequestBody JSONObject obj) {
        String AppId = "wxc73caa6b44bc531c";
        String AppSecret = "2066731c378fd9acdfbc3827aeabfc59";

        JSONObject wxJson = JSONObject.parseObject(WechatTools.getOpenid(obj.getString("code"), AppId, AppSecret));
        System.out.println("微信的返回值{}"+ wxJson);
        //获取openId与session_key
        String openId = wxJson.getString("openid");
        String session_key = wxJson.getString("session_key");
        //新建User对象
        User user = new User();
        user.setUserName("微信用户");
        user.setOpenid(openId);
        user.setSessionKey(session_key);
        //用openId查询数据库，如果不存在，则插入用户信息
        if (userService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getOpenid, openId)) == null) {
            userService.save(user);
        }

        return wxJson.toJSONString();
    }

    //通过openId查询用户信息
    @PostMapping("/getUserInfo")
    public User getUserInfo(@RequestBody String openId) {
        openId = openId.substring(7, openId.length());
        User user = userService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getOpenid, openId));
        System.out.println(user);
        return user;
    }

    //修改用户信息
    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody User user) {
        System.out.println("user:"+user);
        userService.updateById(user);
        return Result.ok();
    }

    //通过微信接口获取用户手机号
    @PostMapping("/getPhoneNumber")
    public Result getPhoneNumber(@RequestBody JSONObject obj) {
        System.out.println("obj:"+obj);
        String token = GetToken();
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + token;
        String result = HttpUtil.post(url, obj.get("code").toString());
        System.out.println("result:"+result);
        return Result.ok().setData(JSONObject.parseObject(result).getJSONObject("phone_info").getString("phoneNumber"));
    }

    private String GetToken() {
        String AppId = "wxc73caa6b44bc531c";
        String AppSecret = "2066731c378fd9acdfbc3827aeabfc59";
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + AppId + "&secret=" + AppSecret;
        String result = HttpUtil.get(url);
        JSONObject jsonObject = JSONObject.parseObject(result);
        return jsonObject.getString("access_token");
    }

}
